#pragma once
#include "myfile.hpp"

using myfile = std::shared_ptr<my_file>;
class file_dir
{
private:
    std::string _name;
    std::unordered_map<std::string, myfile> _id_to_file;
    int _pos;

public:
    file_dir() {}

    file_dir(std::string name) : _name(name)
    {
        _pos = 0;
    }

    ~file_dir() {}

    bool create(std::string user, std::string name)
    {
        // auto ret = _id_to_file.insert(std::make_pair(name, std::make_unique<my_file>(user, name, "file" + std::to_string(_pos++))));
        auto ret = _id_to_file.insert(std::make_pair(name, std::shared_ptr<my_file>(new my_file(user, name, "file" + std::to_string(_pos++)))));

        if (!ret.second)
        {
            error_log("create %s error, is exists", name.c_str());
            return false;
        }
        return true;
    }

    bool dele(std::string name)
    {
        auto ret = _id_to_file.find(name);
        if (ret == _id_to_file.end())
        {
            error_log("%s not exis", name.c_str());
            return false;
        }
        _id_to_file.erase(ret);
        return true;
    }

    void show_file()
    {
        for (auto &e : _id_to_file)
        {
            std::cout << e.second->print() << std::endl;
        }
    }

    std::string read(std::string user, std::string name)
    {
        auto ret = _id_to_file.find(name);
        if (ret == _id_to_file.end())
        {
            error_log("%s not exis", name.c_str());
            return "";
        }

        if (ret->second->master() != user || !ret->second->ret_read())
        {
            return "读取失败";
        }

        return ret->second->get_message();
    }

    bool write(std::string user, std::string name, std::string open)
    {
        auto ret = _id_to_file.find(name);
        if (ret == _id_to_file.end())
        {
            error_log("%s not exis", name.c_str());
            return false;
        }

        if (ret->second->master() != user || !ret->second->ret_write())
        {
            debug_log("写入失败");
            return false;
        }

        std::cout << "输入内容： ";
        std::string str;
        getline(std::cin, str);

        std::string message = ret->second->get_message();
        if (open == "app")
        {
            message += str;
            ret->second->set_message(message);
        }
        else if (open == "in" || open == "")
        {
            ret->second->set_message(str);
        }

        return true;
    }

    void setfile(std::string user, std::string name, std::string str)
    {
        auto ret = _id_to_file.find(name);
        if (ret == _id_to_file.end())
        {
            error_log("%s not exis", name.c_str());
            return;
        }

        if (ret->second->master() != user)
        {
            error_log("你不是所有者,不能更改");
            return;
        }

        // 修改权限
        ret->second->set_file_no();
        if (str.find("r") != std::string::npos)
        {
            ret->second->set_read(1);
        }
        if (str.find("w") != std::string::npos)
        {
            ret->second->set_write(1);
        }
        if (str.find("x") != std::string::npos)
        {
            ret->second->set_run(1);
        }
        return;
    }

    void cur_dir()
    {
        std::cout << _name << std::endl;
    }
};